﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

namespace DataConvertLib
{
	/// <summary>
	/// Bit类型数据转换类
	/// </summary>
	// Token: 0x02000002 RID: 2
	[Description("Bit类型数据转换类")]
	public class BitLib
	{
		/// <summary>
		/// 返回某个字节的指定位
		/// </summary>
		/// <param name="value">字节</param>
		/// <param name="offset">偏移位</param>
		/// <remarks>偏移位0-7有效，否则结果不正确</remarks>
		/// <returns>布尔结果</returns>
		// Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
		[Description("返回某个字节的指定位")]
		public static bool GetBitFromByte(byte value, int offset)
		{
			return ((int)value & 1 << offset) != 0;
		}

		/// <summary>
		/// 获取字节数组(长度为2)中的指定位
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="offset">偏移位</param>
		/// <param name="isLittleEndian">大小端</param>
		/// <remarks>偏移位0-15有效，否则结果不正确</remarks>
		/// <returns>布尔结果</returns>
		// Token: 0x06000002 RID: 2 RVA: 0x00002070 File Offset: 0x00000270
		[Description("获取字节数组(长度为2)中的指定位")]
		public static bool GetBitFrom2Bytes(byte[] value, int offset, bool isLittleEndian = true)
		{
			bool flag = value.Length < 2;
			if (flag)
			{
				throw new ArgumentException("数组长度小于2");
			}
			bool bitFrom2Bytes;
			if (isLittleEndian)
			{
				bitFrom2Bytes = BitLib.GetBitFrom2Bytes(value[1], value[0], offset);
			}
			else
			{
				bitFrom2Bytes = BitLib.GetBitFrom2Bytes(value[0], value[1], offset);
			}
			return bitFrom2Bytes;
		}

		/// <summary>
		/// 获取高低字节的指定位
		/// </summary>
		/// <param name="high">高位字节</param>
		/// <param name="low">低位字节</param>
		/// <param name="offset">偏移位</param>
		/// <remarks>偏移位0-15有效，否则结果不正确</remarks>
		/// <returns>布尔结果</returns>
		// Token: 0x06000003 RID: 3 RVA: 0x000020BC File Offset: 0x000002BC
		[Description("获取高低字节的指定位")]
		public static bool GetBitFrom2Bytes(byte high, byte low, int offset)
		{
			bool flag = offset >= 0 && offset <= 7;
			bool bitFromByte;
			if (flag)
			{
				bitFromByte = BitLib.GetBitFromByte(low, offset);
			}
			else
			{
				bitFromByte = BitLib.GetBitFromByte(high, offset - 8);
			}
			return bitFromByte;
		}

		/// <summary>
		/// 返回字节数组中某个字节的指定位
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="start">字节索引</param>
		/// <param name="offset">偏移位</param>
		/// <remarks>偏移位0-7有效，否则结果不正确</remarks> 
		/// <returns>布尔结果</returns>
		// Token: 0x06000004 RID: 4 RVA: 0x000020F8 File Offset: 0x000002F8
		[Description("返回字节数组中某个字节的指定位")]
		public static bool GetBitFromByteArray(byte[] value, int start, int offset)
		{
			bool flag = start > value.Length - 1;
			if (flag)
			{
				throw new ArgumentException("数组长度不够或开始索引太大");
			}
			return BitLib.GetBitFromByte(value[start], offset);
		}

		/// <summary>
		/// 返回字节数组中某2个字节的指定位
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="start">字节索引</param>
		/// <param name="offset">偏移位</param>
		/// <param name="isLittleEndian">大小端</param>
		/// <remarks>偏移位0-15有效，否则结果不正确</remarks> 
		/// <returns>布尔结果</returns>
		// Token: 0x06000005 RID: 5 RVA: 0x0000212C File Offset: 0x0000032C
		[Description("返回字节数组中某2个字节的指定位")]
		public static bool GetBitFrom2BytesArray(byte[] value, int start, int offset, bool isLittleEndian = true)
		{
			bool flag = start > value.Length - 2;
			if (flag)
			{
				throw new ArgumentException("数组长度不够或开始索引太大");
			}
			byte[] value2 = new byte[]
			{
				value[start],
				value[start + 1]
			};
			return BitLib.GetBitFrom2Bytes(value2, offset, isLittleEndian);
		}

		/// <summary>
		/// 根据一个Short返回指定位
		/// </summary>
		/// <param name="value">short数值</param>
		/// <param name="offset">偏移位</param>
		/// <param name="isLittleEndian">大小端</param>
		/// <remarks>偏移位0-15有效，否则结果不正确</remarks>
		/// <returns>布尔结果</returns>
		// Token: 0x06000006 RID: 6 RVA: 0x00002174 File Offset: 0x00000374
		[Description("根据一个Short返回指定位")]
		public static bool GetBitFromShort(short value, int offset, bool isLittleEndian = true)
		{
			return BitLib.GetBitFrom2Bytes(BitConverter.GetBytes(value), offset, !isLittleEndian);
		}

		/// <summary>
		/// 根据一个UShort返回指定位
		/// </summary>
		/// <param name="value">short数值</param>
		/// <param name="offset">偏移位</param>
		/// <param name="isLittleEndian">大小端</param>
		/// <remarks>偏移位0-15有效，否则结果不正确</remarks>
		/// <returns>布尔结果</returns>
		// Token: 0x06000007 RID: 7 RVA: 0x00002198 File Offset: 0x00000398
		[Description("根据一个UShort返回指定位")]
		public static bool GetBitFromUShort(ushort value, int offset, bool isLittleEndian = true)
		{
			return BitLib.GetBitFrom2Bytes(BitConverter.GetBytes(value), offset, !isLittleEndian);
		}

		/// <summary>
		/// 将字节数组转换成布尔数组
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="length">布尔数组长度</param>
		/// <returns>布尔数组</returns>
		// Token: 0x06000008 RID: 8 RVA: 0x000021BC File Offset: 0x000003BC
		[Description("将字节数组转换成布尔数组")]
		public static bool[] GetBitArrayFromByteArray(byte[] value, int length)
		{
			return BitLib.GetBitArrayFromByteArray(value, 0, length);
		}

		/// <summary>
		/// 将字节数组转换成布尔数组
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="start">开始索引</param>
		/// <param name="length">布尔数组长度</param>
		/// <returns>布尔数组</returns>
		// Token: 0x06000009 RID: 9 RVA: 0x000021D8 File Offset: 0x000003D8
		[Description("将字节数组转换成布尔数组")]
		public static bool[] GetBitArrayFromByteArray(byte[] value, int start, int length)
		{
			bool flag = length <= 0;
			if (flag)
			{
				throw new ArgumentException("长度必须为正数");
			}
			bool flag2 = start < 0;
			if (flag2)
			{
				throw new ArgumentException("开始索引必须为非负数");
			}
			bool flag3 = start + length > value.Length * 8;
			if (flag3)
			{
				throw new ArgumentException("数组长度不够或长度太大");
			}
			BitArray bitArray = new BitArray(value);
			bool[] array = new bool[length];
			for (int i = 0; i < length; i++)
			{
				array[i] = bitArray[i + start];
			}
			return array;
		}

		/// <summary>
		/// 将字节数组转换成布尔数组
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <returns>布尔数组</returns>
		// Token: 0x0600000A RID: 10 RVA: 0x00002268 File Offset: 0x00000468
		[Description("将字节数组转换成布尔数组")]
		public static bool[] GetBitArrayFromByteArray(byte[] value)
		{
			return BitLib.GetBitArrayFromByteArray(value, value.Length * 8);
		}

		/// <summary>
		/// 将一个字节转换成布尔数组
		/// </summary>
		/// <param name="value">字节</param>
		/// <returns>布尔数组</returns>
		// Token: 0x0600000B RID: 11 RVA: 0x00002288 File Offset: 0x00000488
		[Description("将一个字节转换成布尔数组")]
		public static bool[] GetBitArrayFromByte(byte value)
		{
			return BitLib.GetBitArrayFromByteArray(new byte[]
			{
				value
			});
		}

		/// <summary>
		/// 根据位开始和长度截取布尔数组
		/// </summary>
		/// <param name="value">布尔数组</param>
		/// <param name="start">开始索引</param>
		/// <param name="length">长度</param>
		/// <returns>返回布尔数组</returns>
		// Token: 0x0600000C RID: 12 RVA: 0x000022AC File Offset: 0x000004AC
		[Description("根据位开始和长度截取布尔数组")]
		public static bool[] GetBitArrayFromBitArray(bool[] value, int start, int length)
		{
			bool flag = start < 0;
			if (flag)
			{
				throw new ArgumentException("开始索引不能为负数");
			}
			bool flag2 = length <= 0;
			if (flag2)
			{
				throw new ArgumentException("长度必须为正数");
			}
			bool flag3 = value.Length < start + length;
			if (flag3)
			{
				throw new ArgumentException("数组长度不够或开始索引太大");
			}
			bool[] array = new bool[length];
			Array.Copy(value, start, array, 0, length);
			return array;
		}

		/// <summary>
		/// 将字符串按照指定的分隔符转换成布尔数组
		/// </summary>
		/// <param name="value">待转换字符串</param>
		/// <param name="spilt">分割符</param>
		/// <returns>返回布尔数组</returns>
		// Token: 0x0600000D RID: 13 RVA: 0x00002314 File Offset: 0x00000514
		[Description("将字符串按照指定的分隔符转换成布尔数组")]
		public static bool[] GetBitArrayFromBitArrayString(string value, string spilt = " ")
		{
			value = value.Trim();
			List<bool> list = new List<bool>();
			bool flag = value.Contains(spilt);
			if (flag)
			{
				string[] array = value.Split(new string[]
				{
					spilt
				}, StringSplitOptions.RemoveEmptyEntries);
				foreach (string value2 in array)
				{
					list.Add(BitLib.IsBoolean(value2));
				}
			}
			else
			{
				list.Add(BitLib.IsBoolean(value));
			}
			return list.ToArray();
		}

		/// <summary>
		/// 判断是否为布尔
		/// </summary>
		/// <param name="value">布尔字符串</param>
		/// <returns>布尔结果</returns>
		// Token: 0x0600000E RID: 14 RVA: 0x00002398 File Offset: 0x00000598
		[Description("判断是否为布尔")]
		public static bool IsBoolean(string value)
		{
			return value == "1" || value.ToLower() == "true";
		}
	}
}
